package com.itheima.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

//Rest模式
@RestController

public class BookController {

    @GetMapping(value = {"/books", "/"})
    @PreAuthorize("hasAuthority('get::book')")
    public String getById() {
        System.out.println("springboot is running...");
        return "springboot is running...";
    }

    @GetMapping(value = {"/bookss"})
    @PreAuthorize("@ex.hasAuthority('get::book')")
    public String getById1() {
        System.out.println("springboot is running...");
        return "springboot is running...";
    }

    public static void main(String[] args) {
        JwtBuilder jwtBuilder = Jwts.builder().setId("666")//设置id
                .setSubject("testJwt")//主题
                .setIssuedAt(new Date())//签发日期
                .setExpiration(new Date(System.currentTimeMillis() + 10000))
                .signWith(SignatureAlgorithm.HS256, "itlils");
        String jwt = jwtBuilder.compact();
        System.out.println(jwt);

        Claims claims = Jwts.parser().setSigningKey("itlils").parseClaimsJws(jwt).getBody();

        System.out.println(claims);
    }
}
